import 'dart:math';

import 'package:demo/res/resources.dart';
import 'package:demo/widget/app_bar.dart';
import 'package:flutter/material.dart';

class DialogPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _DialogPageState();
  }
}

class _DialogPageState extends State<DialogPage> {
  List<Color> _colorList = [
    Colors.black26,
    Colors.deepOrange,
    Colors.amber,
    Colors.green,
    Colors.greenAccent,
    Colors.lightBlue,
    Colors.deepOrangeAccent,
    Colors.purpleAccent,
    Colors.lightGreenAccent,
    Colors.pink,
    Colors.yellowAccent,
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        centerTitle: 'Dialog样式',
        elevation: 1,
      ),
      body: Column(
        children: <Widget>[
          FlatButton(
            onPressed: () {
              _showDialog();
            },
            color: _colorList[Random().nextInt(_colorList.length)],
            child: Container(
              height: 42,
              width: double.infinity,
              color: _colorList[Random().nextInt(_colorList.length)],
              alignment: Alignment.center,
              child: new Text(
                "样式1",
                style: TextStyle(
                    fontSize: 16,
                    color: Colors.white),
              ),
            ),
          ),
          FlatButton(
            color: _colorList[Random().nextInt(_colorList.length)],
            onPressed: () {
              _showAlertDialog();
            },
            child: Container(
              height: 42,
              width: double.infinity,
              color: _colorList[Random().nextInt(_colorList.length)],
              alignment: Alignment.center,
              child: new Text(
                "样式2",
                style: TextStyle(
                    fontSize: 16,
                    color: Colors.white),
              ),
            ),
          ),
          FlatButton(
            color: _colorList[Random().nextInt(_colorList.length)],
            onPressed: () {
              _showAboutDialog();
            },
            child: Container(
              height: 42,
              width: double.infinity,
              color: _colorList[Random().nextInt(_colorList.length)],
              alignment: Alignment.center,
              child: new Text(
                "样式3",
                style: TextStyle(
                    fontSize: 16,
                    color: Colors.white),
              ),
            ),
          ),
          FlatButton(
            color: _colorList[Random().nextInt(_colorList.length)],
            onPressed: () {
              _showBottomSheet();
            },
            child: Container(
              height: 42,
              width: double.infinity,
              color: _colorList[Random().nextInt(_colorList.length)],
              alignment: Alignment.center,
              child: new Text(
                "样式4",
                style: TextStyle(
                    fontSize: 16,
                    color: Colors.white),
              ),
            ),
          ),
        ],
      ),
    );
  }

  void _showDialog() {
    showDialog(
      context: context,
      barrierDismissible: false,
      child: new SimpleDialog(
        contentPadding: const EdgeInsets.all(10),
        title: new Text("I am Simple Dialog"),
        children: <Widget>[
          new Text("我是内容区域，哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈哈哈"),
          new ListTile(
            leading: new Icon(Icons.apps),
            title: new Text("apps"),
          ),
          new ListTile(
            leading: new Icon(Icons.android),
            title: new Text("andrpid"),
          ),
          new ListTile(
            leading: new Icon(Icons.cake),
            title: new Text("cake"),
          ),
          new ListTile(
            leading: new Icon(Icons.local_cafe),
            title: new Text("cofe"),
          ),
        ],
      ),
    );
  }

  void _showAlertDialog() {
    var alertDialog = new AlertDialog(
      title: new Text("This is AlertDialog"),
      content: new Text("This is Content"),
      actions: <Widget>[
        new FlatButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: new Text("Cancel"),
        ),
        new FlatButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: new Text(
            "Confirm",
            style: new TextStyle(color: Colors.red),
          ),
        ),
      ],
    );

    showDialog(
        context: context,
        builder: (context) {
          return alertDialog;
        });
  }

  void _showBottomSheet() {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return new Container(
            child: new Padding(
              padding: const EdgeInsets.all(10),
              child: new Column(
                children: <Widget>[
                  new ListTile(
                    leading: new Icon(Icons.chat),
                    title: new Text("开始会话"),
                  ),
                  new ListTile(
                    leading: new Icon(Icons.help),
                    title: new Text("操作说明"),
                  ),
                  new ListTile(
                    leading: new Icon(Icons.settings),
                    title: new Text("系统设置"),
                  ),
                  new ListTile(
                    leading: new Icon(Icons.more),
                    title: new Text("更多设置"),
                  ),
                ],
              ),
            ),
          );
        });
  }

  void _showAboutDialog() {
    showDialog(
        context: context,
        builder: (context) {
          return AboutDialog(
            applicationName: "Flutter Demo：",
            applicationVersion: "v1.0",
            applicationLegalese: "https://www.pgyer.com/Do0i",
            applicationIcon: new Icon(
              Icons.android,
              color: Colorful.color_state,
            ),
            children: <Widget>[new Text("更新摘要\n提交Gitlab\n定义后续功能")],
          );
        });
  }
}
